package jp.co.dailyReport.search;

import java.util.ArrayList;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.search.sql.SearchWeeklyReportLoadExecuter;
import jp.co.dailyReport.search.SearchConditionOfWeeklyReport;
import jp.co.dailyReport.weeklyReport.WeeklyReport;
import jp.co.dailyReport.weeklyReport.WeeklyReportManager;
import jp.co.dailyReport.PermissionManager;

/**
 * 週報検索結果情報管理クラス
 */
public class SearchWeeklyReportManager {

	private static Logger log = Logger.getLogger(SearchWeeklyReportManager.class);

	/**
	 * 社員の権限で週報検索結果情報を取得します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param searchConditionOfWeeklyReport 週報検索条件オブジェクト
	 * @return 週報検索結果オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static SearchWeeklyReport[] load(Staff staff,SearchConditionOfWeeklyReport searchConditionOfWeeklyReport) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		SearchWeeklyReportLoadExecuter searchWeeklyReportLoadExecuter = new SearchWeeklyReportLoadExecuter(searchConditionOfWeeklyReport);
		Object objs[] = (Object[])null;
		ArrayList answer = new ArrayList();

		try {
            objs = ExecuterManager.execute(connectionFactory, searchWeeklyReportLoadExecuter);
		} catch(SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}

		if(objs == null || objs.length == 0) {
			return new SearchWeeklyReport[0];
		}

		SearchWeeklyReport[] searchWeeklyReport = (SearchWeeklyReport[])objs;
		for(int i =0; i < searchWeeklyReport.length;i++){
			WeeklyReport weeklyReport = WeeklyReportManager.load(searchWeeklyReport[i].getWeeklyReportId());
			if(PermissionManager.hasPermission(staff,weeklyReport,PermissionManager.METHOD_READ)){
				answer.add(searchWeeklyReport[i]);
			}
		}

		return (SearchWeeklyReport[])answer.toArray(new SearchWeeklyReport[0]);
    }
}
